package junit;

import static org.junit.Assert.*;
import businessobjects.Question;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class QuestionTestHibernate {

	static SessionFactory sessionFactory;
	static ServiceRegistry serviceRegistry;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		
		
		Configuration  conf = new Configuration().configure();
		serviceRegistry = new ServiceRegistryBuilder().applySettings(
				conf.getProperties()).buildServiceRegistry();
		sessionFactory = conf.buildSessionFactory(serviceRegistry);

	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
		sessionFactory.close();
	}

	@Test
	public void saveAutoObject() {

		Session session = sessionFactory.openSession();
		Transaction trans = session.beginTransaction();
		Question question = new Question();
		question.setKategorie("qgu");
		question.setQuestion("Funktioniert dieser Test?");
		question.setAnswer("Hoffentlich... :-/");
		session.save(question); // save POJO, mapping can be found in
							// Question.hbm.xml
		trans.commit();
		session.close();
		assertTrue(question.getId() > 0);
		assertEquals("Kategorie", "qgu", question.getKategorie());
		assertEquals("Frage", "Funktioniert dieser Test?", question.getQuestion());
		assertEquals("Antwort", "Hoffentlich... :-/", question.getAnswer());
		

		
	}
}
